package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.cache.service.ILoginInfoCacheService;
import cn.wolfcode.luowowo.cache.service.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

@Controller
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IVerifyCodeCacheService verifyCodeCacheService;
    @Reference
    private ILoginInfoCacheService loginInfoCacheService;

    @RequestMapping("/checkPhone")
    @ResponseBody
    //手机号验证
    public Object checkPhone(String phone) {
        return userInfoService.checkPhone(phone);
    }

    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    //发送验证码
    public AjaxResult sendVerifyCode(String phone) {
        AjaxResult result = new AjaxResult();
        //获取验证码
        String verifyCode4Redis = verifyCodeCacheService.getVerifyCode(phone);
        //判断是否已经发送过
        if (verifyCode4Redis != null) {
            return result.mark("请不要在60s内重复发送验证码");
        }
        //生成验证码
        String verifyCode = UUID.randomUUID().toString().substring(0, 4);
        System.out.println(phone + "的验证码为:" + verifyCode);

        //存入Redis中
        try {
            verifyCodeCacheService.setVerifyCode(phone, verifyCode);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/userRegist")
    @ResponseBody
    //用户注册
    //分布式远程调用情况下,Dubbo会对我们抛出的异常做特殊处理,没有异常信息message,所以直接返回ajaxResult对象
    public AjaxResult userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        return userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
    }

    @RequestMapping("/userLogin")
    @ResponseBody
    //用户登录
    public AjaxResult userLogin(String phone, String password, HttpServletResponse response, HttpSession session) {
        //执行登录,将用户信息存入Redis中,并返回ajaxResult对象
        AjaxResult result = userInfoService.userLogin(phone, password);
        //登录成功,返回cookie给客户端
        String token = (String) result.getData();
        Cookie cookie = new Cookie(Consts.USER_INFO_TOKEN, token);
        //设置cookie的作用域
        cookie.setPath("/");
        response.addCookie(cookie);
        //获取redis的登录信息,存入session
        String loginInfo = loginInfoCacheService.getLoginInfo(token);
        System.out.println(loginInfo);
        session.setAttribute(Consts.USER_INFO, JSON.parseObject(loginInfo, UserInfo.class));
        System.out.println(session.getAttribute(Consts.USER_INFO));
        return result;
    }


}
